package com.ysm.smfu.activity;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysm.smfu.MainActivity;
import com.ysm.smfu.R;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.Md5Utils;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.PayPwdEditText;
import com.ysm.smfu.welcome.SharedConfig;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/14.
 */
public class ZhiFuPsdActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.zhifu_clear)
    TextView zhifuClear;
    @BindView(R.id.fragment_zhifu_psd)
    PayPwdEditText fragmentZhifuPsd;
    @BindView(R.id.alert_psd)
    TextView alertPsd;
    @BindView(R.id.zhifu_text)
    TextView zhifu_text;
    @BindView(R.id.alert_forget)
    TextView alert_forget;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivity_topbar_img;
    // 第一次输入密码
    String string;
    // 第二次输入密码
    String string2;
    boolean isFirst = true;
    private String userId;
    private String xiugai;
    private String name;
    private String code;

    @Override
    protected void init() {
        super.init();
        xiugai = getIntent().getStringExtra(SpKey.INTENT);
        code = getIntent().getStringExtra(SpKey.CODE);
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        userId = currShared.getString(SpKey.USERID, "");
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_zhifu);
        ButterKnife.bind(this);
        mainActivity_topbar_img.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(xiugai))
            titleBody.setText("修改支付密码");
        else
            titleBody.setText("设置支付密码");
        fragmentZhifuPsd.initStyle(R.drawable.edit_num_bg, 6, 0.5f, R.color.color999999, R.color.color999999, 20);

        zhifu_text.setText("请设置支付密码");
        alert_forget.setVisibility(View.GONE);
        //        输入完成时调用
        fragmentZhifuPsd.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {
                if (isFirst) {
                    string = str;
                    isFirst = false;
                    fragmentZhifuPsd.clearText();
                    zhifu_text.setText("请再次输入支付密码");
                } else {
                    string2 = fragmentZhifuPsd.getPwdText();
                    if (!TextUtils.equals(string, string2)) {
                        alertPsd.setText("两次密码必须一致");
                        fragmentZhifuPsd.clearText();
                        return;
                    }
                    alertPsd.setText("");
                    String md5psd = Md5Utils.encode(string2);
                    xiuGaiZhiFuPsd(md5psd);

                }
            }
        });
    }

    /**
     * 修改支付密码
     * /user/updatePayPwd
     * id: 用户id
     * payPwd:支付密码
     * type:验证码类型 检测状态state==1,添加type,state==0,不添加type
     *
     * @param psd
     */
    private void xiuGaiZhiFuPsd(String psd) {
        BaseProtocal.builder(true).changeZhiFuPsd(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                String state = JsonUtil.getState(response);
                if (!TextUtils.equals(state, "1")) {
                    alertPsd.setText(JsonUtil.getMessage(response));
                    return;
                }
                if (TextUtils.isEmpty(xiugai))
                    showErrorDialog("设置成功");
                else
                    showErrorDialog("修改成功");
            }
            @Override
            public void onFail(Exception e) {
                ToastUtils.showToast("修改失败：" + e.getMessage());
            }
        }, userId, psd, xiugai, code);
    }

    @OnClick({R.id.zhifu_clear, R.id.back, R.id.alert_forget})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zhifu_clear:
                fragmentZhifuPsd.clearText();
                break;
            case R.id.back:
                if (!isFirst) {
                    zhifu_text.setText("请输入支付密码");
                    fragmentZhifuPsd.clearText();
                    string = "";
                    string2 = "";
                    isFirst = true;
                } else {
                    finish();
                }
                break;
        }
    }

    private void showErrorDialog(String msg) {
        android.support.v7.app.AlertDialog dialog = new android.support.v7.app.AlertDialog.Builder(this)
                .setMessage(msg).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //                返回到“我的”页面
                        if (!TextUtils.isEmpty(xiugai)) {
                            UIUtils.startActivity(MainActivity.class);
                            finish();
                        } else {
                            UIUtils.startActivity(PayActivity.class);
                            finish();
                        }
                    }
                }).create();
        dialog.show();
    }
}
